import struct

import Utils.Utils

class BSAIndexEntry:
    NAME_LENGTH = 12
    SIZE_LENGTH = 2
    UNKNOWNS_LENGTH = 2
    
    def __init__(self, name = '', size = 0, offset = 0, has_override = False):
        self._name = name
        self._size = size
        self._offset = offset
        self._has_override = has_override
    
    def __repr__(self):
        return "BSAIndexEntry: [" + self._name + "|size=" +\
        str(self._size) + "|offset=" + str(self._offset) +\
        "|has_override=" + str(self._has_override) + "]"
    
    def __cmp__(self, other):
        return cmp(self._name, other.get_name())
    
    # getters
    def get_name(self):
        return self._name
    
    def get_size(self):
        return self._size
    
    def get_offset(self):
        return self._offset
    
    def get_has_override(self):
        return self._has_override
    
    # setters
    def set_name(self, name):
        self._name = name
        
    def set_size(self, size):
        self._size = size
        
    def set_offset(self, offset):
        self._offset = offset
    
    def set_has_override(self, has_override):
        self._has_override = has_override

    def read(self, file):
        has_override = False
        
        nm = file.read(self.NAME_LENGTH)

        self._name = Utils.Utils.strip_unprintable(nm)

        file.read(self.UNKNOWNS_LENGTH)
        
        sz = file.read(self.SIZE_LENGTH)
        self._size = struct.unpack('H', sz)[0]

        file.read(self.UNKNOWNS_LENGTH)
        self._has_override = has_override